<template>
    <div>
        <div>
            <el-input
                    class="inputDiv"
                    size="small"
                    placeholder="请输入职位"
                    @keydown.enter.native="addPosition"
                    v-model="pos.name">
            </el-input>
            <el-button type="primary" size="small" icon="el-icon-circle-plus" @click="addPosition">添加</el-button>
            <el-button type="danger" size="small" style="margin-left: 250px" :disabled="multipleSelection == 0" @click="multiDelete">批量删除</el-button>
        </div>
        <div class="posManaMain">
            <el-table
                    element-loading-text="拼命加载中"
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    border
                    stripe
                    @selection-change="handleSelectionChange"
                    size="small"
                    :data="positions"
                    style="width: 60%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间"
                        >
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="状态"
                >
                    <template slot-scope="scope" >
                        <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
                        <el-tag type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                title="编辑"
                :visible.sync="dialogVisible"
                width="30%">
            <div>
                <el-tag
                        type="success"
                        effect="dark">
                        职位名称
                </el-tag>
                <el-input size="medium" class="updateInput" v-model="updateData.name"></el-input>
                <br>
                <el-tag type="success" effect="dark"style="margin-top: 8px">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</el-tag>
                <el-switch
                        style="margin-left: 8px"
                        v-model="updateData.enabled"
                        active-text="开启"
                        inactive-text="禁用">
                </el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="medium" @click="dialogVisible = false">取 消</el-button>
                <el-button size="medium" type="primary" @click="updateButton">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Posi",
        data(){
            return {
                multipleSelection:[

                ],
                dialogVisible: false,
                pos:{
                    name:"",
                    enabled:false
                },
                updateData:{
                    name:"",
                    enabled:false
                },
                positions:[

                ]
            }
        },
        mounted() {
            this.initPositions();
        },
        methods:{
            multiDelete(){
                this.$confirm('此操作将删除【'+this.multipleSelection.length+'】个职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item=>{
                        ids += "ids=" + item.id + '&';
                    })
                    this.deleteRequest("/system/basic/pos/"+ids).then(resp=>{
                        if(resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            addPosition(){
                this.postRequest("/system/basic/pos/",this.pos).then(resp=>{
                    if(resp){
                        this.initPositions();
                        this.pos.name=''
                    }
                })
            },
            handleEdit(index,data){
                Object.assign(this.updateData,data);
                this.dialogVisible = true;
            },
            updateButton(){
                this.putRequest("/system/basic/pos/",this.updateData).then(resp=>{
                    if(resp){
                        this.initPositions();
                        this.dialogVisible = false;
                        this.updateData.name='';
                    }
                })

            },
            handleDelete(index,data){
                this.$confirm('此操作将删除【'+data.name+'】职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/"+data.id).then(resp=>{
                        if(resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initPositions(){
                this.getRequest("/system/basic/pos/").then(resp=>{
                    this.positions = resp
                })
            }
        }
    }
</script>

<style>
    .updateInput {
        width: 300px;
        margin-left: 8px;
    }
    .inputDiv {
        margin-right: 10px;
        width: 300px
    }
    .posManaMain {
        margin-top: 10px;
    }

</style>